home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / Miracle C Compiler / miricleC compiler.msi / Instal01.cab / _C0FA132CFC094381912286389FE19C8C < prev    next >
Encoding:
Text File  |  2000-07-19  |  2.5 KB  |  84 lines

  1. #include <io.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4.  
  5. void xio();
  6.  
  7. int main()
  8. {
  9.     xio();
  10. }
  11.  
  12. void xio()
  13. {
  14.     int fd,buflen;
  15.     char buf1[100], buf2[100];
  16.  
  17.     printf("==> starting xio <==\n");
  18.  
  19.     // --- test the ZFILEIO functions
  20.     // ------------------------------
  21.     remove("testdata.txt");
  22.  
  23.     // --- first we test the creat system call
  24.     assert((fd=creat("testdata.txt"))!=EOF);
  25.     assert(fd>4);    // --- 0..4 are DOS standard streams
  26.  
  27.     strcpy(buf1,"ground control to major tom");
  28.     assert(write(fd,buf1,strlen(buf1))==strlen(buf1));
  29.     assert(close(fd)==0);
  30.  
  31.     // --- now we test open system call, and creat for pre-existing file
  32.     assert((fd=open("testdata.txt",O_RDONLY))!=EOF);
  33.     assert(fd>4);    // --- 0..4 are DOS standard streams
  34.     assert((buflen=strlen(buf1))>0);
  35.     assert(read(fd,buf1,100)==buflen);
  36.     assert(close(fd)==0);
  37.  
  38.     assert((fd=open("testdata.txt",O_RDWR))!=EOF);
  39.     assert(fd>4);    // --- 0..4 are DOS standard streams
  40.     assert(read(fd,buf1,100)==buflen);
  41.     strcpy(buf2,"\r\nzowie bowie\tziggy marley\r\n");
  42.     assert(write(fd,buf2,strlen(buf2))==strlen(buf2));
  43.     assert(close(fd)==0);
  44.  
  45.     assert((fd=open("testdata.txt",O_RDWR))!=EOF);
  46.     assert(fd>4);    // --- 0..4 are DOS standard streams
  47.     assert(read(fd,buf1,100)==buflen+strlen(buf2));
  48.     assert(strncmp(buf1,"ground control to major tom\r\nzowie bowie\tziggy marley\r\n",buflen+strlen(buf2))==0);
  49.     assert(close(fd)==0);
  50.  
  51.     assert((fd=open("testdata.txt",O_WRONLY))!=EOF);
  52.     assert(fd>4);    // --- 0..4 are DOS standard streams
  53.     assert(read(fd,buf1,100)==EOF);
  54.     assert(close(fd)==0);
  55.  
  56.     // --- now we test creat for pre-existing file
  57.     assert((fd=creat("testdata.txt"))!=EOF);
  58.     assert(fd>4);    // --- 0..4 are DOS standard streams
  59.  
  60.     strcpy(buf1,"anemone is the enemy");
  61.     assert(write(fd,buf1,strlen(buf1))==strlen(buf1));
  62.     assert(close(fd)==0);
  63.  
  64.     // --- we have now tested all types of open, creat, close, read, write
  65.     // --- let us try lseek
  66.     assert((fd=open("testdata.txt",O_RDWR))!=EOF);
  67.     assert(fd>4);    // --- 0..4 are DOS standard streams
  68.     buflen=strlen("anemone is the enemy");
  69.     assert(read(fd,buf1,100)==buflen);
  70.  
  71.     assert((int)lseek(fd,0L,SEEK_CUR)==buflen);
  72.     assert((int)lseek(fd,0L,SEEK_END)==buflen);
  73.     assert((int)lseek(fd,0L,SEEK_SET)==0);
  74.     assert((int)lseek(fd,5L,SEEK_SET)==5);
  75.     assert(close(fd)==0);
  76.  
  77.     assert(remove("testdata.txt")==0);    // --- now you see it
  78.     assert(remove("testdata.txt")==-1);    // --- now you don't
  79.     printf("passed open/close/creat/read/write/lseek/remove...\n");
  80.  
  81.     printf("==> finished xio <==\n");
  82.     return;
  83. }
  84.